﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimpleMediaManager
{
    public partial class Add : Form
    {
        private bool edit;
        private string search;

        public TMDB.Movie SelectedMovie { get; set; }
        public TMDB.Movie[] SearchResults { get; set; }

        public Add(bool edit = false, string search = null, TMDB.Movie[] results = null)
        {
            InitializeComponent();

            this.edit = edit;
            this.search = search;
            this.SearchResults = results;
        }

        void tmdb_MovieSearchCompleted(object sender, TMDB.MovieSearchCompletedEventArgs e)
        {
            SearchResults = e.Movies;
            foreach (TMDB.Movie movie in e.Movies)
            {
                listViewResults.Items.Add(new ListViewItem(new string[] { movie.Name, movie.Released.Value.Year.ToString() }));
            }

            Program.Tmdb.MovieSearchCompleted -= new TMDB.Api.MovieSearchAsyncCompletedEventHandler(tmdb_MovieSearchCompleted);

            if (listViewResults.Items.Count > 0) buttonOk.Enabled = true;
            buttonCancel.Enabled = true;
            buttonSearch.Enabled = true;
            pictureBoxLoader.Visible = false;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Program.Tmdb.MovieSearchCompleted += new TMDB.Api.MovieSearchAsyncCompletedEventHandler(tmdb_MovieSearchCompleted);
            Program.Tmdb.MovieSearchAsync(textBoxSearch.Text);
            listViewResults.Items.Clear();
            buttonCancel.Enabled = false;
            buttonOk.Enabled = false;
            buttonSearch.Enabled = false;
            pictureBoxLoader.Visible = true;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonOk.Enabled = (listViewResults.SelectedIndices.Count == 1);
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            SelectedMovie = SearchResults[listViewResults.SelectedIndices[0]];

            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            buttonSearch.Enabled = (textBoxSearch.Text.Length > 0);
        }

        private void Add_Load(object sender, EventArgs e)
        {
            this.Text = (edit) ? "Edit Movie" : "Add Movie";
            textBoxSearch.Text = search;

            if (this.SearchResults != null)
            {
                foreach (TMDB.Movie movie in SearchResults)
                {
                    ListViewItem lvi = new ListViewItem(movie.Name);
                    lvi.SubItems.Add(movie.Released.Value.Year.ToString());
                    listViewResults.Items.Add(lvi);
                }
            }
        }
    }
}
